/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_lang_defs.h"
#include "nu_rt_prog_ctx.h"
#include "nu_stmt_class.h"
#include "nu_functions.h"
#include "nu_error_codes.h"
#include "nu_expr_literal.h"

/* -------------------------------------------------------------------------- */

namespace nu
{


/* -------------------------------------------------------------------------- */

stmt_class_t::stmt_class_t( prog_ctx_t & ctx, const std::string& id )
   :
   stmt_t(ctx), _id(id)
{

   //Get a reference to global function set
   auto & funcs = functions_t::get_instance();

   //Search class by name
   //
   auto i = ctx.class_list.find(id);

   syntax_error_if(
      (i != ctx.class_list.end() /*&&
       i->second.first.get_line() != ctx.compiletime_pc.get_line()*/),
      PROGLANG_STMT_CLASS" '" + id + "' already defined");

   ctx.class_list.insert( id );

   ctx.parsing_scope.push(PROGLANG_STMT_CLASS);
   ctx.parsing_class.push(id);
}


/* -------------------------------------------------------------------------- */

void stmt_class_t::run(rt_prog_ctx_t & ctx)
{
   auto & subctx = ctx.class_metadata;
   auto handle = subctx.begin_find(ctx.runtime_pc);

   rt_error_code_t::get_instance().throw_if(
      !handle,
      ctx.runtime_pc.get_line(),
      rt_error_code_t::E_CLASS_UNDEF,
      _id);

#if 0
   auto scope_id = ctx.proc_scope.get_scope_id();

   // Skip function body if we are in the global scope
   if (scope_id.empty())
   {
      handle->flag[instrblock_t::EXIT] = true;
      ctx.go_to(handle->pc_end_stmt);
      return;
   }

   else
   {
      handle->flag[instrblock_t::EXIT] = false;
   }

#endif

   handle->flag[instrblock_t::EXIT] = false;
   ctx.go_to_next();
}


/* -------------------------------------------------------------------------- */

stmt_class_t::stmt_cl_t stmt_class_t::get_cl() const throw()
{
   return stmt_cl_t::CLASS_BEGIN;
}


} // namespace nu


/* -------------------------------------------------------------------------- */

